package com.yayun.merchant.service.impl;

import com.yayun.common.utils.DateUtils;
import com.yayun.merchant.domain.AuctionItem;
import com.yayun.merchant.domain.AuctionRecord;
import com.yayun.merchant.mapper.AuctionRecordMapper;
import com.yayun.merchant.service.IAuctionItemService;
import com.yayun.merchant.service.IAuctionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AuctionRecordServiceImpl implements IAuctionRecordService {
    @Autowired
    private AuctionRecordMapper auctionRecordMapper;

    @Autowired
    private IAuctionItemService auctionItemService;

    /**
     * 查询竞拍记录
     */
    @Override
    public AuctionRecord selectAuctionRecordById(Long id) {
        return auctionRecordMapper.selectAuctionRecordById(id);
    }

    /**
     * 查询竞拍记录列表
     */
    @Override
    public List<AuctionRecord> selectAuctionRecordList(AuctionRecord auctionRecord) {
        return auctionRecordMapper.selectAuctionRecordList(auctionRecord);
    }

    /**
     * 新增竞拍记录
     */
    @Override
    public int insertAuctionRecord(AuctionRecord auctionRecord) {
        auctionRecord.setCreateTime(DateUtils.getNowDate());
        return auctionRecordMapper.insertAuctionRecord(auctionRecord);
    }

    /**
     * 修改竞拍记录
     */
    @Override
    public int updateAuctionRecord(AuctionRecord auctionRecord) {
        return auctionRecordMapper.updateAuctionRecord(auctionRecord);
    }

    /**
     * 批量删除竞拍记录
     */
    @Override
    public int deleteAuctionRecordByIds(Long[] ids) {
        return auctionRecordMapper.deleteAuctionRecordByIds(ids);
    }

    /**
     * 删除竞拍记录信息
     */
    @Override
    public int deleteAuctionRecordById(Long id) {
        return auctionRecordMapper.deleteAuctionRecordById(id);
    }

    /**
     * 用户出价
     */
    @Override
    @Transactional
    public int placeBid(Long auctionId, Long userId, BigDecimal bidPrice) {
        // 查询拍卖商品信息
        AuctionItem auctionItem = auctionItemService.selectAuctionItemById(auctionId);
        if (auctionItem == null || auctionItem.getStatus() != 1) {
            return 0;
        }

        // 检查出价是否有效
        AuctionRecord highestBid = selectHighestBidRecord(auctionId);
        if (highestBid != null) {
            if (bidPrice.compareTo(highestBid.getBidPrice().add(auctionItem.getStepPrice())) < 0) {
                return 0;
            }
            // 更新之前的最高出价状态为出局
            updateAuctionRecordStatus(auctionId, 1, 0);
        } else {
            if (bidPrice.compareTo(auctionItem.getStartPrice()) < 0) {
                return 0;
            }
        }

        // 创建新的竞拍记录
        AuctionRecord record = new AuctionRecord();
        record.setAuctionId(auctionId);
        record.setUserId(userId);
        record.setBidPrice(bidPrice);
        record.setStatus(1); // 领先

        // 更新拍卖商品当前价格
        auctionItem.setCurrentPrice(bidPrice);
        auctionItemService.updateAuctionItem(auctionItem);

        return insertAuctionRecord(record);
    }

    /**
     * 查询拍卖商品的最高出价记录
     */
    @Override
    public AuctionRecord selectHighestBidRecord(Long auctionId) {
        return auctionRecordMapper.selectHighestBidRecord(auctionId);
    }

    /**
     * 查询用户的竞拍记录
     */
    @Override
    public List<AuctionRecord> selectUserAuctionRecords(Long userId) {
        return auctionRecordMapper.selectUserAuctionRecords(userId);
    }

    /**
     * 更新拍卖商品相关记录状态
     */
    @Override
    public int updateAuctionRecordStatus(Long auctionId, Integer oldStatus, Integer newStatus) {
        return auctionRecordMapper.updateAuctionRecordStatus(auctionId, oldStatus, newStatus);
    }
} 